<div id="admin-links">
	<a href="index.php?s=team-management">Teams Management</a>
	<a href="index.php?s=team-management&a=1">New Team</a>
	<a href="index.php?s=team-management&a=5">Import File</a>
</div>

<div id="content">
<?php
	$a = REPLACE_Injection($_GET["a"]);
	if($a == 1){
		?>
		<h1>Team Creation</h1>
		<form action="index.php?s=team-management&a=2" method="post">
		<table border="0" cellspacing="0" cellpadding="0" class="form">
			<tr>
				<td><strong>Team Name</strong></td>
				<td><input type="text" maxlength="30" size="50" name="name" /></td>
			</tr>
			<tr>
				<td><strong>League</strong></td>
				<td>
					<select name="league">
						<?php 
						$query = new mysqlQuery("tournament", "", "ligas", "", "`secundaria`='0'", "", "");
						$query->execute();
						while($row = $query->next()){
							echo '<option value="'.$row["id"].'">'.$row["nombre"].'</option>';
						}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td><strong>Secondary League</strong></td>
				<td>
					<select name="secondary_league">
						<?php 
						$query = new mysqlQuery("tournament", "", "ligas", "", "`secundaria`='1'", "", "");
						$query->execute();
						echo '<option value="0">None</option>';
						while($row = $query->next()){
							echo '<option value="'.$row["id"].'">'.$row["nombre"].'</option>';
						}
						?>
					</select>
				</td>
			</tr>
			<tr><td colspan="2"><input type="submit" value="Create Team" class="boton"/></td></tr>
		</table>
		</form>
		<?php
	}else if($a == 2){
		if(!$nombre = REPLACE_Injection($_POST["name"]))
			$error = "The team name is not an optional fileld";
		if(!$liga = REPLACE_Injection($_POST["league"]))
			$error = "The league is not an optional field";
		$liga_secundaria = REPLACE_Injection($_POST["secondary_league"]);
		if(!empty($liga_secundaria) && ($liga == $liga_secundaria))
			$error = "The league and the secondary league must be different";
		if(sizeof($error) == 0){
			$values = array(NULL, $nombre, $liga, $liga_secundaria, 0, 0, 0, 0, 0, "");
			$insert = new mysqlInsert("tournament", "", "equipos", $values);
			$insert->execute();
			?>
			<h1>Team Creation</h1>
			<div id="ok">The team has been created.</div>
			<a href="index.php?s=team-management" class="back">Back to Management</a>
			<?php
		}else{
			?>			
			<h1>Team Creation</h1>
			<div id="error">You have the following errors: <br><br><?php echo $error;?></div>
			<a href="index.php?s=team-management&a=1" class="back">Back to Team Creation</a>
			<?php
		}
	}else if($a == 3){
		?>
		<h1>Team Edition</h1>
		<?php
			$id = REPLACE_Injection($_GET["id"]);
			$query = new mysqlQuery("tournament", "", "equipos", "", "`id`='".$id."'", "", "");
			$query->execute();
			if($query->getCount() == 1){
				$row = $query->next();
				$nombre = $row["nombre"];
				?>
				<form action="index.php?s=team-management&a=4&id=<?php echo $id;?>" method="post">
				<table border="0" cellspacing="0" cellpadding="0" class="form">
					<tr>
						<td><strong>Team Name</strong></td>
						<td><input type="text" maxlength="30" size="50" name="name" value="<?php echo $nombre;?>" /></td>
					</tr>
					<tr>
						<td><strong>League</strong></td>
						<td>
							<select name="league">
								<?php 
								$query = new mysqlQuery("tournament", "", "ligas", "", "`secundaria`='0'", "", "");
								$query->execute();
								while($row2 = $query->next()){
									if($row["liga"] == $row2["id"])
										echo '<option value="'.$row2["id"].'" selected>'.$row2["nombre"].'</option>';
									else
										echo '<option value="'.$row2["id"].'">'.$row2["nombre"].'</option>';
								}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<td><strong>Secondary League</strong></td>
						<td>
							<select name="secondary_league">
								<?php 
								$query = new mysqlQuery("tournament", "", "ligas", "", "`secundaria`='1'", "", "");
								$query->execute();
								echo '<option value="0">None</option>';
								while($row2 = $query->next()){
									if($row["liga_secundaria"] == $row2["id"])
										echo '<option value="'.$row2["id"].'" selected>'.$row2["nombre"].'</option>';
									else
										echo '<option value="'.$row2["id"].'">'.$row2["nombre"].'</option>';
								}
								?>
							</select>
						</td>
					</tr>
					<tr><td colspan="2"><input type="submit" value="Edit Team" class="boton"/></td></tr>
				</table>
				</form>
				<?php
			}else{
				echo '<div id="error">Team does not exist</div>';
			}
	}else if($a == 4){
		$id = REPLACE_Injection($_GET["id"]);		
		if(!$nombre = REPLACE_Injection($_POST["name"]))
			$error = "The team name is not an optional fileld";
		if(!$liga = REPLACE_Injection($_POST["league"]))
			$error = "The league is not an optional field";
		
		$liga_secundaria = REPLACE_Injection($_POST["secondary_league"]);
		if(!empty($liga_secundaria) && ($liga == $liga_secundaria))
			$error = "The league and the secondary league must be different";
		
		if(sizeof($error) == 0){
			$values = array("nombre", "liga", "liga_secundaria");
			$data = array($nombre, $liga, $liga_secundaria);
			$update = new mysqlUpdate("tournament", "", "equipos", $values, $data, "`id`='".$id."'");
			$update->execute();
			?>
			<h1>Team Edition</h1>
			<div id="ok">The team has been modified..</div>
			<a href="index.php?s=team-management" class="back">Back to Management</a>
			<?php
		}else{
			?>			
			<h1>Team Edition</h1>
			<div id="error">You have the following errors: <br><br><?php echo $error;?></div>
			<a href="index.php?s=team-management&a=3&id=<?php echo $id;?>" class="back">Back to Team Edition</a>
			<?php
		}
	}else if($a == 5){
		?>
		<h1>Import Teams And Leagues From File</h1>
		<form action="index.php?s=team-management&a=6" method="post" enctype="multipart/form-data">
		<table border="0" cellspacing="0" cellpadding="0" class="form">
			<tr>
				<td><strong>Upload a File to Import</strong></td>
				<td><input type="file" name="file"/></td>
			</tr>
			<tr><td colspan="2">The Imported file must be a TXT file with the following format:<br/>Team Name, League Name<br/>Team Name, League Name<br/>[...]</td></tr>
			<tr><td colspan="2"><input type="submit" value="Import" class="boton"/></td></tr>
		</table>
		</form>
		<?php
	}else if($a == 6){		
		?>
		<h1>Import Teams And Leagues From File</h1>
		<?php 
		if(!is_uploaded_file($_FILES['file']['tmp_name'])){
			$error = "You have to upload a file!<br>";
		}else{
			if($_FILES['file']['type'] == "text/plain"){
				$file = fopen($_FILES['file']['tmp_name'], "r");
				$contenido = fread($file, filesize($_FILES['file']['tmp_name']));
				if($contenido){
					echo "<div id='ok'>File Accessed succesfully, Processing...</div>";
					$equipos = explode("\n", $contenido);
					for($i = 0; $i < sizeof($equipos); $i++){
						$equipos[$i] = trim($equipos[$i]);
						if(strpos($equipos[$i],",")){
							$equipo = explode(",",$equipos[$i]);
							$equipo[0] = trim($equipo[0]);
							$equipo[1] = trim($equipo[1]);
							$query = new mysqlQuery("tournament", "", "ligas", "", "`nombre`='".$equipo[1]."'", "", "");
							$query->execute();
							if($query->getCount() != 1)
							 	$error .= "In process error: The league '".$equipo[1]."' does not exist in line '".$equipos[$i]."'<br>";
							else{
								$row = $query->next();
								$values = array(NULL, $equipo[0], $row["id"]);
								$insert = new mysqlInsert("tournament", "", "equipos", $values);
								$insert->execute();
							}
						}else{
							$error .= "In process error: There wasnt any comma in the line '".$equipos[$i]."<br>";
						}
					}
					
				}else{
					$error .= "Error in accessing file<br>";
				}
			}else{
				$error .= "Error in accessing file<br>";
			}
		}
		
		if(strlen($error) == 0){
			echo "<div id='ok'>File imported successfully</div>";
		}else{
			echo "<div id='error'>".$error."</div>";
		}
	}else{
		?>
		<h1>Team Management</h1>
		
		<table border="0" cellspacing="0" cellpadding="0" class="table">
			<tr>
				<td class="title">Team Name</td>
				<td class="title">League Name</td>
				<td class="title">Secondary League</td>
				<td class="title" colspan="1">Operations</td>
			</tr>
			<?php 
				$q = "SELECT * FROM ((SELECT e.id as id, l1.nombre as liga, 
											NULL as liga_secundaria, 
											e.nombre as equipo 
							FROM ligas as l1, equipos as e 
							WHERE e.liga = l1.id AND e.liga_secundaria = '0') 
						UNION (SELECT e.id as id, l1.nombre as liga, 
											l2.nombre as liga_secundaria, 
											e.nombre as equipo 
							FROM ligas as l1, ligas as l2, equipos as e 
							WHERE e.liga = l1.id AND e.liga_secundaria = l2.id)) AS Q";				
				$query = new mysqlQuery("tournament", $q, "", "", "", "", "");
				$query->execute();
				$n = 0;
				while($row = $query->next()){
					echo '<tr>';
					echo '<td class="td'.(($n%2)+1).'"><strong>'.$row["equipo"].'</strong></td>';
					echo '<td class="td'.(($n%2)+1).'"><strong>'.$row["liga"].'</strong></td>';
					echo '<td class="td'.(($n%2)+1).'"><strong>'.$row["liga_secundaria"].'</strong></td>';
					echo '<td class="td'.(($n%2)+1).'"><a href="index.php?s=team-management&a=3&id='.$row["id"].'">Edit</a></td>';
					//echo '<td class="td'.(($n%2)+1).'"><a href="index.php?s=league-management&a=5&id='.$row["id"].'">Delete</a></td>';
					echo '</tr>';
					$n++;
				}				
			?>
		</table>
		<?php 
	}
?>
</div>